package com.leoandroid.tool.toolsbox

import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.leoandroid.tool.toolsbox.bean.FengJingBean
import com.leoandroid.tool.toolsbox.largeread.R
import com.leoandroid.tool.toolsbox.largeread.databinding.ActivityFengJingBinding
import com.leoandroid.tool.toolsbox.net.NetCallBack
import com.leoandroid.tool.toolsbox.net.NetRequest
import org.json.JSONObject

class FengJingActivity : AppCompatActivity() {

    var bindingF: ActivityFengJingBinding? = null
    val binding get() = bindingF!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindingF = ActivityFengJingBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.includeTitle.titleTv.text = "景区查询"
        binding.includeTitle.backIv.setOnClickListener {
            finish()
        }
        binding.searchTv.setOnClickListener {
            var word = binding.edit.text
            val url =
                "http://apis.juhe.cn/fapigx/scenic/query?key=6a3420c1a431ef2bc1384163fa52249f&word=${word}"
            Log.d("pppppp", "url>>" + url);
            NetRequest.asyncGet(url, object : NetCallBack {
                override fun onFailure(code: Int, e: Throwable?) {

                }

                override fun onSuccess(json: JSONObject?) {
                    Log.d("xxxxxxx", "json>>" + json.toString())
                    val result = json?.getJSONObject("result")
                    if (result == null) {
                        Toast.makeText(this@FengJingActivity, "查询不到", Toast.LENGTH_SHORT).show()
                        return
                    }
                    val list = result?.getJSONArray("list")
                    val fengjing = list?.getJSONObject(0)
                    runOnUiThread {
                        val fengjingBean =
                            Gson().fromJson(fengjing.toString(), FengJingBean::class.java)
                        binding.provinceTv.text = "省份：" + fengjingBean.province
                        binding.cityTv.text = "城市：" + fengjingBean.city
                        binding.nameTv.text = "景区：" + fengjingBean.name
                        binding.contentTv.text = "介绍：" + fengjingBean.content
                        binding.bgIv.setImageResource(R.mipmap.fengjing)
                    }
                }
            })
        }

    }
}